<!--
*@Author: QWP
*@Description: 个人信息模版导出
*@Date: 2024-10-09 14:38:47
-->
<template>
  <u-dialog
    title="个人信息导出"
    :hideBtns="['submit']"
    placement="right"
    v-bind="$attrs">
    <a-layout class="f-h-100">
      <caption class="f-font-center f-font-size24">职工基本情况表</caption>
      <table cellpadding="100" border="1"
        class="u-info-table">
        <thead>
          <tr>
            <td class="f-font-right f-font-size16" colspan="8">表格生成日期：{{ dayjs().format('YYYY年MM月DD日') }}</td>
          </tr>
        </thead>
        <tbody>
          <tr v-for="row in userInfo">
            <td v-for="col in row" v-bind="col">{{ col.label || '' }}</td>
          </tr>
        </tbody>
      </table>
    </a-layout>
  </u-dialog>
</template>

<script lang='ts' setup>
import { ref, reactive, computed, onMounted, watch, useAttrs } from 'vue' 
import { useBaseStore } from '@/store'
import { storeToRefs } from 'pinia'
import { useRoute, useRouter } from 'vue-router'
import { personInfoApi } from '@/service/personalSet' 
import { TColumns, moreSearchEl, equiRenderCol, formatEnum } from './data/colData.ts'
import dayjs from 'dayjs'
import { numberC, alertC, messageC, nullC, isValue } from '@/util/common'

const baseStore = useBaseStore()

const attrs = useAttrs()
const props = defineProps<{
  current: object
}>()

const userInfo = reactive([
  [
    {label: '姓名', key: '',},
    {label: '', key: 'personnelName',},
    {label: '性别', key: '', style: { width: '150px' }},
    {label: '', key: 'gender', type: 'enum', data: baseStore.storesexs},
    {label: '出生年月及年龄', key: '', style: { width: '150px' }},
    {label: '', isIndex: false,
      key: [
        { key: 'dateOfBirth', type: 'date', format: 'YYYY年MM月' },
        { key: 'perAge', unit: '岁'},
      ],
    },
    {label: '照片', key: '', rowspan: 3,
      style: { 'text-align': 'center' }
    },
  ],
  [
    {label: '民族', key: '', colspan: 1, rowspan: 1},
    {label: '', key: 'nation', colspan: 1, rowspan: 1},
    {label: '籍贯', key: '', colspan: 1, rowspan: 1},
    {label: '', key: 'nativePlace', colspan: 1, rowspan: 1},
    {label: '政治面貌', key: '', colspan: 1, rowspan: 1},
    {label: '', key: 'politicalStatus', colspan: 1, rowspan: 1, type: 'enum', data: baseStore.storepolicys },
  ],
  [
    {label: '参加工作时间', key: '',colspan: 1, rowspan: 1},
    {label: '', key: 'employmentTime', colspan: 1, rowspan: 1, type: 'date', format: 'YYYY-MM-DD'},
    {label: '到单位工作年月', key: '',colspan: 1, rowspan: 1},
    {label: '', key: 'employmentYears',colspan: 1, rowspan: 1, type: 'date', format: 'YYYY-MM'},
    {label: '入党时间', key: '',colspan: 1, rowspan: 1},
    {label: '', key: 'groupTime',colspan: 1, rowspan: 1, type: 'date', format: 'YYYY-MM-DD'},
  ],
  [
    {label: '本人联系电话', key: '',colspan: 1, rowspan: 1},
    {label: '', key: 'perPhone', colspan: 1, rowspan: 1},
    {label: '身份证号', key: '',colspan: 1, rowspan: 1},
    {label: '', key: 'idCardNo', colspan: 1, rowspan: 1},
    {label: '所属部门', key: '', colspan: 1, rowspan: 1},
    {label: '', key: 'departmentId', colspan: 2, rowspan: 1, type: 'enum', data: baseStore.deptData},
  ],
  [
    {label: '专业技术职称', key: '',colspan: 1, rowspan: 1,},
    {label: '', colspan: 2, rowspan: 1,
      origin: 'perTechnicals',
      key: [
        { key: 'technicalLevle', type: 'enum', data: baseStore.storetechLevels, },
        { key: 'technicalProfession', }
      ]
    },
    {label: '', key: '', colspan: 1, rowspan: 1,},
    {label: '其他资格证书', key: '',colspan: 1, rowspan: 1},
    {label: '', colspan: 2, rowspan: 1,  
      origin: 'perCredentials',
      key: [
        { key: 'credentialName' }
      ]
    },
  ],
  [
    {label: '受聘专家', key: '',colspan: 1, rowspan: 1},
    {label: '', colspan: 4, rowspan: 1,
      origin: 'perAppointedExperts',
      key: [
        { key: 'expertName' },
        { key: 'expertGroup', type: 'enum', data: baseStore.storeexpertGroup },
      ]
    },
    {label: '紧急联系人及及电话', key: '',colspan: 1, rowspan: 1},
    {label: '', colspan: 2, rowspan: 1,
      origin: 'perPhones',
      key: [
        { key: 'phoneName' },
        { key: 'phoneNub',},
      ]
    },
  ],
  [
    {label: '学历学位', key: '',colspan: 1, rowspan: 3},
    {label: '初始学历', key: '', colspan: 1, rowspan: 1},
    {label: '', key: 'degreeNameInit', colspan: 2, rowspan: 1,
      origin: 'perDegreeInitVos', type: 'enum', data: baseStore.storeeducations
    },
    {label: '毕业院校系及专业', key: '',colspan: 1, rowspan: 1},
    {label: '', colspan: 2, rowspan: 1,
      origin: 'perDegreeInitVos', 
      key: [
        { key: 'schoolInit' },
        { key: 'professionInit' },
      ]
    },
  ],
  [
    {label: '在职学历', key: '', colspan: 1, rowspan: 1},
    {label: '', key: 'degreeNameJob', colspan: 2, rowspan: 1,
      // style: { 'text-align': 'center' },
      origin: 'perDegreeJobVos',
      type: 'enum', 
      data: baseStore.storeeducations
    },
    {label: '毕业院校系及专业', key: '',colspan: 1, rowspan: 1},
    {label: '', colspan: 2, rowspan: 1,
      origin: 'perDegreeJobVos', 
      key: [
        { key: 'schoolJob' },
        { key: 'professionJob' },
      ]
    },
  ],
  [
    {label: '最高学历', key: '', colspan: 1, rowspan: 1},
    {label: '', key: 'degreeNameMax', colspan: 2, rowspan: 1,
      // style: { 'text-align': 'center' },
      origin: 'perDegreeMaxVos',
      type: 'enum', 
      data: baseStore.storeeducations
    },
    {label: '最高学历形式', key: '',colspan: 1, rowspan: 1},
    {label: '', key: 'degreeFormMax', colspan: 2, rowspan: 1,
      origin: 'perDegreeMaxVos',
      type: 'enum', data: baseStore.storedegreeForm
    },
  ],
  [
    {label: '现任职务', key: '', colspan: 2, rowspan: 1},
    {label: '', key: 'job', colspan: 2, rowspan: 1,
      origin: 'perJobs',
    },
    {label: '职务批准时间', key: '',colspan: 1, rowspan: 1},
    {label: '', key: 'approvalDate', colspan: 3, rowspan: 1,
      origin: 'perJobs', type: 'date', format: 'YYYY-MM-DD',
      filter: (o) => o.isPresentPosition == 1
    },
  ],
  [
    {label: '简历', key: '', colspan: 1, rowspan: 1,
      style: { 'height': '200px'}
    },
    {label: '', key: 'curriculumVitae', colspan: 6, rowspan: 1, },
  ],
  [
    {label: '受表彰和受处分情况', colspan: 1, rowspan: 1,
      style: { 'height': '200px'},
    },
    {label: '', colspan: 6, rowspan: 1,
      origin: ['perCommends', 'perPunishments'],
      key: [
        {key: 'commendUnit',},
        {key: 'commendDate', type: 'date', format: 'YYYY-MM-DD'},
        {key: 'commendName', },
        {key: 'commendLevel', type: 'enum', data: baseStore.storeawardLevels},

        {key: 'punUnit',},
        {key: 'punishmentDate', type: 'date', format: 'YYYY-MM-DD'},
        {key: 'punishmentName'},
      ]
    },
  ],
  [
    {label: '科研情况', key: '', colspan: 1, rowspan: 1,
      style: { 'height': '200px'}
    },
    {label: '',  colspan: 6, rowspan: 1,
      origin: 'perScientificResearches',
      key: [
        {key: 'scientificResearchType', type: 'enum', data: baseStore.storescientificResearchType},
        {key: 'resRemark', },
      ]
    },
  ],
  [
    {label: '年度考核情况', key: '', colspan: 1, rowspan: 1,
      style: { 'height': '200px'}
    },
    {label: '', colspan: 6, rowspan: 1,
      origin: 'perAnnualAssessments',
      key: [
        {key: 'annualAssessmentDate', type: 'date', format: 'YYYY年'},
        {key: 'annualAssessmentLevle', type: 'enum', data: baseStore.storeannualAssessmentLevle},
      ]
    },
  ],
  [
    {label: '家庭主要成员及重要社会关系', key: '', colspan: 1, rowspan: 2,
      style: { 'width': '100px'}
    },
    {label: '与本人关系', key: '', colspan: 1, rowspan: 1, },
    {label: '姓名', key: '', colspan: 1, rowspan: 1, },
    {label: '出生年月', key: '', colspan: 1, rowspan: 1, },
    {label: '政治面貌', key: '', colspan: 1, rowspan: 1, },
    {label: '工作单位及职务', colspan: 2, rowspan: 1, },
  ],
  [
    {label: '', key: '', colspan: 1, rowspan: 1,},
    {label: '', key: '', colspan: 1, rowspan: 1,},
    {label: '', key: '', colspan: 1, rowspan: 1,},
    {label: '', key: '', colspan: 1, rowspan: 1,},
    {label: '', key: '', colspan: 2, rowspan: 1,},
  ],
])

// 数据回显
const echoInfo = async () => {
  // formLoading.value = true;
  try {
    const res = await personInfoApi.query(props.current['keyid'])
    if(res.code == 0) {
      userInfo.forEach(item => {
        item.forEach(cItem => {
          
          if(cItem['key']) {
          
            let originData = cItem.origin ? (res.data[cItem.origin] || []) : res.data

            if(cItem.origin instanceof Array) {
              originData = cItem.origin.map(o => res.data[o] || []).flat()
            }

            if(originData instanceof Array == false) {
              originData = [ originData ]
            }

            if(cItem['filter']) {
              originData = originData.filter(fo => cItem.filter(fo))
            }
            
            if(cItem['key'] instanceof Array) {
              cItem.label = originData.map((da, daIndex) => {
                return `
                  ${ cItem['isIndex'] == true || originData.length > 1 ? (daIndex + 1) + '.' : '' }
                  ${
                    (cItem['key'] || []).map(c => formatDate(c, da[c['key']]) + (c['unit'] || ''))
                                        .filter(i => isValue(i)).join('-')
                  }
                `
              }).join('\n');
            }
            else {
              cItem.label = originData.map(da => formatDate(cItem, da[cItem['key']])).join('-');
            }
          }
        })
      })

      // 家庭单位
      echoFamilys(res.data['perFamilies'] || [])
    }
  } catch (error) { }
  // formLoading.value = false;
}

const echoFamilys = (datas) => {
  let keys = [
    { key: 'famRelation', origin: 'perFamilies'  },
    { key: 'famName', origin: 'perFamilies'},
    { key: 'famBirthDate',  origin: 'perFamilies', type: 'date', format: 'YYYY年MM月'},
    { key: 'famPolitcalStatus', origin: 'perFamilies',},
    { origin: 'perFamilies', key: [
        { key: 'famUnit' },
        { key: 'famJob' },
      ]
    }
  ]
  let result = datas.map(item => {
    let results = []
    keys.forEach((k, kindex) => {
      const { key } = k;
      let colspan = kindex == keys.length - 1 ? 2 : 1
      if(k.type == 'date') {
        results.push({label: item[key] ? dayjs(item[key]).format(k.format) : '', colspan, rowspan: 1})
      }
      else if(k.type == 'enum') {
        results.push({label: formatEnum(item[key], k.data), colspan, rowspan: 1})
      }
      else {
        let label = key instanceof Array ? key.map(i => item[i]).join('-') : (item[key] || '')
        results.push({label: label, colspan, rowspan: 1,})
      }
    })
    return results
  })
  let index = userInfo.findIndex(item => item.some(o => o.label == '家庭主要成员及重要社会关系'));
  if(index != -1) {
    let currentArr = userInfo[index]
    currentArr[0].rowspan = result.length + 1
    userInfo.splice(index, 1, currentArr)
    userInfo.splice(index + 1, userInfo.length, ...result)
  }
}

const formatDate = (item, value) => {
  let cValue = value === 0 ? value.toString() : value
  let tValue = cValue || ''
  tValue = tValue || `${tValue}`
  if(item.type == 'date') {
    return tValue ? dayjs(tValue).format(item.format) : ''
  }
  else if(item.type == 'enum') {
    return tValue ? formatEnum(tValue, item['data'] || []) : ''
  }
  else {
    return tValue ? tValue : ''
  }
}

watch(() => attrs.open, (nval, oval) => {
  if(nval == true) {
    echoInfo()
  }
})

// const getKData =

</script>

<style lang='scss' scoped>
.f-font-center {
  text-align: center
}
.f-font-right {
  text-align: right
}
.u-info-table {
  width: 80%;
  margin: 0 auto;
  th, td {
    padding: 10px;
  }
}
</style>